/******************************************************************
 *
 *    Java Lib For Android, Powered By Shenzhen Jiuzhou.
 *
 *    Copyright (c) 2001-2014 Digital Telemedia Co.,Ltd
 *    http://www.d-telemedia.com/
 *
 *    Package:     com.etc.role.service
 *
 *    Filename:    RoleService.java
 *
 *    Description: TODO(用一句话描述该文件做什么)
 *
 *    Copyright:   Copyright (c) 2001-2014
 *
 *    Company:     Digital Telemedia Co.,Ltd
 *
 *    @author:     Administrator
 *
 *    @version:    1.0.0
 *
 *    Create at:   2016年3月24日 下午9:38:10
 *
 *    Revision:
 *
 *    2016年3月24日 下午9:38:10
 *        - first revision
 *
 *****************************************************************/
package com.etc.role.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.etc.common.exception.DataOperationException;
import com.etc.common.exception.SysOperationException;
import com.etc.common.util.CustomBeanUtils;
import com.etc.module.model.ModuleOper;
import com.etc.role.dao.IRoleMapper;
import com.etc.role.model.Role;
import com.etc.role.service.IRoleService;
import com.etc.role.vo.RoleVO;
import com.github.pagehelper.PageHelper;


/**
 * @ClassName RoleService
 * @Description TODO(这里用一句话描述这个类的作用)
 * @author 张亮
 * @Date 2016年3月24日 下午9:38:10
 * @version 1.0.0
 */
@Service("roleService")
public class RoleServiceImpl implements IRoleService {
    
    Logger log = LoggerFactory.getLogger(RoleServiceImpl.class);
    
    @Autowired
    private IRoleMapper roleMapper;

    /* (非 Javadoc)
     * Description:
     * @see com.etc.common.service.IBaseService#insert(com.etc.common.model.BaseModel)
     */
    @Override
    public Integer insert(RoleVO vo) {
        int res=0;
        Role role=new Role();
        try {
            CustomBeanUtils.copyProperties(role, vo);
            res=roleMapper.insert(role);
        } catch (Exception e) {
            String errMsg = String.format("role保存异常,参数 :vo=%s", vo);
            log.error(errMsg, e);
            throw new DataOperationException(errMsg);
        } 
        return res;
    }

    /* (非 Javadoc)
     * Description:
     * @see com.etc.common.service.IBaseService#delete(java.io.Serializable)
     */
    @Override
    public void delete(Integer id) {
        try {
            roleMapper.delete(id);
        } catch (Exception e) {
            String errMsg = String.format("role删除异常,参数:id=%s", id);
            log.error(errMsg, e);
            throw new DataOperationException(errMsg);
        }
    }

    /* (非 Javadoc)
     * Description:
     * @see com.etc.common.service.IBaseService#update(com.etc.common.model.BaseModel)
     */
    @Override
    public void update(RoleVO vo) {
        Role role=new Role();
        try {
            CustomBeanUtils.copyProperties(role, vo);
            roleMapper.update(role);
        } catch (Exception e) {
            String errMsg = String.format("role修改异常,参数:vo=%s", vo);
            log.error(errMsg, e);
            throw new DataOperationException(errMsg);
        } 
    }

    /* (非 Javadoc)
     * Description:
     * @see com.etc.common.service.IBaseService#selectById(java.io.Serializable)
     */
    @Override
    public RoleVO selectById(Integer id) {
        RoleVO roleVO=new RoleVO();
        try {
            Role role=roleMapper.selectById(id);
            CustomBeanUtils.copyProperties(roleVO, role);
        } catch (Exception e) {
            String errMsg = String.format("role查询异常,参数:id=%s", id);
            log.error(errMsg, e);
            throw new SysOperationException(errMsg);
        }
        
        return roleVO;
    }

    /* (非 Javadoc)
     * Description:
     * @see com.etc.common.service.IBaseService#list(java.util.Map)
     */
    @Override
    public List<RoleVO> list(Map<String, Object> param) {
        try {
            PageHelper.startPage((Integer) param.get("pageNo"), (Integer) param.get("pageNum"));
            List<Role> modelList = roleMapper.list(param);
            List<RoleVO> voList = new ArrayList<RoleVO>();
            for (Role model : modelList) {
                RoleVO vo=new RoleVO();
                CustomBeanUtils.copyProperties(vo, model);
                voList.add(vo);
            }
            return voList;
        } catch (Exception e) {
            String errMsg = String.format("role查询异常,参数 :param=%s", param);
            log.error(errMsg, e);
            throw new SysOperationException(errMsg);
        }
    }

    /* (非 Javadoc)
     * Description:
     * @see com.etc.common.service.IBaseService#count(java.util.Map)
     */
    @Override
    public int count(Map<String, Object> param) {
        return roleMapper.count(param);
    }

    @Override
    public int insertRoleOper(RoleVO roleVO, List<ModuleOper> moduleOpers) {
        int res=0;
        try {
            Map<String, Object> param=new HashMap<>();
            Role role=new Role();
            CustomBeanUtils.copyProperties(role, roleVO);
            param.put("role", role);
            param.put("list", moduleOpers);
            //删除当前角色权限
            int id=role.getTsrId();
            roleMapper.deleteRoleOper(id);
            //批量插入角色权限
            res=roleMapper.insertRoleOper(param);
        } catch (Exception e) {
            String errMsg = String.format("批量插入角色权限异常,参数 :param=%s", moduleOpers);
            throw new SysOperationException(errMsg);
        } 
        return res;
    }

}
